package com.yyw.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyw.Service.WordService;
import com.yyw.bean.Word;
import com.yyw.dao.WordDao;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "每日一词API")
@RestController
@RequestMapping("/user")
@Slf4j
public class WordController {

    @Value("${my.session.user}")
    private String userAttr;

    @Autowired
    private WordService wordService;

    @Autowired
    private WordDao wordDao;

    // 修改单词
    @CrossOrigin
    @ApiOperation(value = "修改单词", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "单词id", required = true),
            @ApiImplicitParam(name = "word", value = "单词", required = true),
            @ApiImplicitParam(name = "content", value = "中文", required = true) })
    @PostMapping("/updateWord")
    public Result updateWord(@RequestParam("id") Long id, @RequestParam("word") String word,
            @RequestParam("content") String content) {

        Word word2 = new Word(id, word, content, null);
        if (id == null || StringUtils.isEmpty(word2.getWord()) || StringUtils.isEmpty(word2.getContent())) {
            return ResultFactory.notice("数据不合法");
        }

        int result = wordDao.updateWord(word2);
        if (result > 0) {
            return ResultFactory.success("修改成功");
        } else {
            return ResultFactory.failure("修改失败");
        }
    }

    // 删除单词
    @CrossOrigin
    @ApiOperation(value = "删除单词", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "单词id", required = true) })
    @PostMapping("/deleteWord")
    public Result deleteWord(@RequestParam("id") Long id) {
        if (id == null) {
            return ResultFactory.notice("数据不合法");
        }
        int i = wordDao.deleteWord(id);
        if (i > 0) {
            return ResultFactory.success("删除成功");
        } else {
            return ResultFactory.failure("删除失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "添加新单词", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "word", value = "单词", required = true),
            @ApiImplicitParam(name = "content", value = "中文", required = true) })
    @PostMapping("/saveWord")
    public Result saveWord(@RequestParam("word") String word, @RequestParam("content") String content) {
        Word w = new Word(null, word, content, null);
        return wordService.saveWord(w);
    }

    @CrossOrigin
    @ApiOperation(value = "分页获取单词", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false) })
    @GetMapping("/getWords")
    public Result getWords(@RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        return wordService.getWords(pageNo, pageNum);
    }
}
